package com.laivi.basic.module.orm;

import java.lang.reflect.Field;
import java.util.List;

import com.laivi.basic.annotation.orm.Table;
import com.laivi.basic.model.orm.Cnd;
import com.laivi.basic.model.sql.BasicDB;
import com.laivi.basic.model.sql.Page;
import com.laivi.basic.module.log.Log;
import com.laivi.basic.module.log.Logs;

/**
 * @author laivi.zhu@gmail.com
 * @date 2014-7-9
 * description:
 */
public class Dao extends ADao {
	private Log log=Logs.getLog(Dao.class);
	
	public Dao(BasicDB db){
		super(db);
	}
	
	public Dao(String url,String user,String password){
		super(url,user,password);
	}

	@Override
	public <T> void add(T entity) {
		Table table=entity.getClass().getAnnotation(Table.class);
		if(table!=null){
			dbDao.add(table.value(), this.mor(entity));
		}
		
	}

	@Override
	public <T> long insert(T entity) {
		Table table=entity.getClass().getAnnotation(Table.class);
		if(table!=null){
			return dbDao.insert(table.value(), this.mor(entity));
		}
		return 0;
	}

	@Override
	public <T> void update(T entity) {
		Table table=entity.getClass().getAnnotation(Table.class);
		if(table!=null){
			try {
				Field field=entity.getClass().getField("id");
				dbDao.update(table.value(), this.mor(entity),"id="+field.getLong(entity));
			} catch (NoSuchFieldException e) {
				log.error(":"+e.getMessage());
			} catch (SecurityException e) {
				log.error(":"+e.getMessage());
			} catch (IllegalArgumentException e) {
				log.error(":"+e.getMessage());
			} catch (IllegalAccessException e) {
				log.error(":"+e.getMessage());
			}
			
		}
	}

	@Override
	public <T> void delete(T entity) {
		Table table=entity.getClass().getAnnotation(Table.class);
		if(table!=null){
			try {
				Field field=entity.getClass().getField("id");
				dbDao.delete(table.value(), "id="+field.getLong(entity));
			} catch (NoSuchFieldException e) {
				log.error(":"+e.getMessage());
			} catch (SecurityException e) {
				log.error(":"+e.getMessage());
			} catch (IllegalArgumentException e) {
				log.error(":"+e.getMessage());
			} catch (IllegalAccessException e) {
				log.error(":"+e.getMessage());
			}
		}
	}
	
	@Override
	public <T> void delete(long id,Class<T> klass) {
		Table table=klass.getAnnotation(Table.class);
		if(table!=null){
			dbDao.delete(table.value(), "id="+id);
		}
	}

	@Override
	public <T> T get(long id,Class<T> klass) {
		Table table=klass.getAnnotation(Table.class);
		if(table!=null){
			return this.orm(dbDao.get(table.value(), null, "id="+id, null), klass);
		}
		return null;
	}
	
	@Override
	public <T> T get(Cnd cnd,Class<T> klass){
		Table table=klass.getAnnotation(Table.class);
		if(table!=null){
			return this.orm(dbDao.get(table.value(), null, cnd.toCnd(), null), klass);
		}
		return null;
	}

	@Override
	public <T> List<T> query(Cnd cnd, Class<T> klass) {
		Table table=klass.getAnnotation(Table.class);
		if(table!=null){
			return this.orm(dbDao.query(table.value(), null, cnd.toCnd(), null), klass);
		}
		return null;
	}

	@Override
	public <T> Page<T> query(Cnd cnd, Class<T> klass, int pageNum, int pageSize) {
		Table table=klass.getAnnotation(Table.class);
		if(table!=null){
			return this.orm(dbDao.query(table.value(), null, cnd.toCnd(), null,pageNum,pageSize), klass);
		}
		return null;
	}

	@Override
	public <T> int count(Cnd cnd, Class<T> klass) {
		Table table=klass.getAnnotation(Table.class);
		if(table!=null){
			return dbDao.count(table.value(), cnd.toCnd());
		}
		return 0;
	}

	@Override
	public <T> Object fun(String fun, String field, Cnd cnd, Class<T> klass) {
		Table table=klass.getAnnotation(Table.class);
		if(table!=null){
			return dbDao.fun(table.value(), cnd.toCnd(), fun, field);
		}
		return null;
	}

}
